package com.zatech.cgnci.project.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zatech.cgnci.project.base.enums.ResponseCodeEnum;
import com.zatech.cgnci.project.base.exception.GlobalException;
import com.zatech.cgnci.project.base.utils.VerifyUtils;
import com.zatech.cgnci.project.entity.CgnciBusinessInsuranceDO;
import com.zatech.cgnci.project.entity.CgnciBusinessProjectDO;
import com.zatech.cgnci.project.entity.CgnciDirectInsuranceDO;
import com.zatech.cgnci.project.entity.CgnciDirectInsuranceRiskDO;
import com.zatech.cgnci.project.mapper.CgnciDirectInsuranceRiskMapper;
import com.zatech.cgnci.project.service.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 直保险种表 服务实现类
 * </p>
 *
 * @author Generator
 * @since 2025-03-19
 */
@Service
public class CgnciDirectInsuranceRiskServiceImpl extends ServiceImpl<CgnciDirectInsuranceRiskMapper, CgnciDirectInsuranceRiskDO> implements CgnciDirectInsuranceRiskService {

    @Resource
    CgnciDirectInsuranceService directInsuranceService;
    @Resource
    CgnciDirectInsuranceRiskService directInsuranceRiskService;
    @Resource
    CgnciBusinessProjectService businessProjectService;
    @Resource
    CgnciBusinessInsuranceService businessInsuranceService;
    @Resource
    CgnciReinsuranceInfoService reinsuranceInfoService;
    @Resource
    CgnciReinsuranceRiskService reinsuranceRiskService;
    @Resource
    CgnciReinsuranceRiskInsuredService reinsuranceRiskInsuredService;
    @Resource
    CgnciReinsurerService reinsurerService;
    @Resource
    CgnciUnderwritingPolicyService underwritingPolicyService;
    @Resource
    CgnciBusinessStatService businessStatService;

    @Override
    public void deleteInsuranceRisk(Long riskId, String type) {
        // 查询直保险种
        CgnciDirectInsuranceRiskDO insuranceRiskDO = this.getById(riskId);
        if (VerifyUtils.isEmpty(insuranceRiskDO)) {
            throw new GlobalException(ResponseCodeEnum.HTTP_REQUEST_SERVICE_ERROR, "riskId有误");
        }
        if (VerifyUtils.isEmpty(insuranceRiskDO.getRiskSubTypeCode())) {
            throw new GlobalException(ResponseCodeEnum.HTTP_REQUEST_SERVICE_ERROR, "直保险种riskSubTypeCode为空");
        }

        // 根据直保险种id查询对应的直保信息
        CgnciDirectInsuranceDO directInsuranceDO = directInsuranceService.getById(insuranceRiskDO.getDirectInsuranceId());
        if (VerifyUtils.isEmpty(directInsuranceDO)) {
            throw new GlobalException(ResponseCodeEnum.HTTP_REQUEST_SERVICE_ERROR, "未找到直保信息");
        }

        // 处理直保信息汇总字段
        directInsuranceService.updateDirectInsuranceColumn(insuranceRiskDO, directInsuranceDO, null);
        // 删除直保险种
        this.baseMapper.deleteById(insuranceRiskDO.getRiskId());


        // 查询业务号
        Long businessId = null;
        switch (type) {
            case "1":
                CgnciBusinessProjectDO businessProjectDO = businessProjectService.getById(directInsuranceDO.getBusinessId());
                businessId = businessProjectDO.getProjectId();
                break;
            case "2":
                CgnciBusinessInsuranceDO businessInsuranceDO = businessInsuranceService.getById(directInsuranceDO.getBusinessId());
                businessId = businessInsuranceDO.getInsuranceId();
                break;
            default:
        }

        // 删除分人险种
        reinsuranceRiskService.deleteReinsuranceRisk("1", businessId, insuranceRiskDO.getRiskSubTypeCode());

        // 删除超净自留险种
        underwritingPolicyService.deleteUnderwritingPolicyRisk(businessId, insuranceRiskDO.getRiskSubTypeCode());

        // 删除临分分出险种
        reinsuranceRiskService.deleteReinsuranceRisk("3", businessId, insuranceRiskDO.getRiskSubTypeCode());

        // 删除合约分出险种
        reinsuranceRiskService.deleteHyReinsuranceRisk("2", businessId, insuranceRiskDO.getRiskSubTypeCode());

        // 删除统计字段
        businessStatService.deleteBusinessStat(businessId, insuranceRiskDO.getRiskSubTypeCode());

    }
}
